package com.lzm.ydpt.module.mall.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.byt.framlib.base.BaseActivity;
import com.byt.framlib.basemvp.boby.FormBodys;
import com.byt.framlib.commonwidget.LoadingTip;
import com.byt.framlib.commonwidget.onclicktwo.AntiShake;
import com.byt.framlib.commonwidget.refresh.RefreshHeaderView;
import com.lzm.ydpt.Constants;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.mall.ShopInfoBean;
import com.lzm.ydpt.mvp.contract.ShopInfoDetailContract;
import com.lzm.ydpt.mvp.presenterimpl.ShopInfoDetailPresenterImpl;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 商戶詳情
 */
public class ShopInfoDetailActivity extends BaseActivity<ShopInfoDetailPresenterImpl> implements ShopInfoDetailContract.View {

    @BindView(R.id.tv_shop_info_name)
    TextView tv_shop_info_name;
    @BindView(R.id.tv_shop_info_follow)
    TextView tv_shop_info_follow;
    @BindView(R.id.tv_shop_follers_num)
    TextView tv_shop_follers_num;
    @BindView(R.id.tv_shop_distance_num)
    TextView tv_shop_distance_num;
    @BindView(R.id.tv_shop_price_select)
    TextView tv_shop_price_select;
    @BindView(R.id.img_select_price_state)
    ImageView img_select_price_state;
    @BindView(R.id.ll_shop_peice_select)
    LinearLayout ll_shop_peice_select;
    @BindView(R.id.tv_shop_buy_select)
    TextView tv_shop_buy_select;
    @BindView(R.id.tv_shop_fiter_select)
    TextView tv_shop_fiter_select;
    @BindView(R.id.ltp_shop_info_detail)
    LoadingTip ltp_shop_info_detail;
    @BindView(R.id.srf_rv_shop_info_detail)
    RefreshLayout srf_rv_shop_info_detail;
    @BindView(R.id.rv_shop_info_detail)
    RecyclerView rv_shop_info_detail;

    private long info_id = 0L;
    private ShopInfoBean mShopInfoBean = null;

    @Override
    public int getLayoutId() {
        return R.layout.activity_shop_info_details;
    }

    @Override
    public ShopInfoDetailPresenterImpl initPreData() {
        return new ShopInfoDetailPresenterImpl(this);
    }

    @Override
    public void initView() {
        info_id = getIntent().getLongExtra(Constants.INP_SHOP_ID, 0L);
        initRv();
        ltp_shop_info_detail.setLoadingTip(LoadingTip.LoadStatus.loading);
        fetchShopInfoDetails();
    }

    private void initRv() {
        setRefreshLayout(srf_rv_shop_info_detail);
        srf_rv_shop_info_detail.setEnableLoadMore(false);
        srf_rv_shop_info_detail.setRefreshHeader(new RefreshHeaderView(this).getHeaderStyleStaffF4());
        srf_rv_shop_info_detail.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                fetchShopInfoDetails();
            }
        });
    }

    /**
     * 获取店铺详情
     */
    private void fetchShopInfoDetails() {
        FormBodys.Builder formBodys = new FormBodys.Builder();
        formBodys.add("store_id", info_id);
        mPresenter.onShopInfo(formBodys.build());
    }


    @OnClick({R.id.ll_show_shop_name, R.id.tv_shop_info_follow, R.id.img_shop_info_delete, R.id.ll_shop_peice_select, R.id.tv_shop_buy_select, R.id.tv_shop_fiter_select})
    public void OnClick(View v) {
        if (AntiShake.check(v.getId())) {
            return;
        }
        switch (v.getId()) {
            case R.id.ll_show_shop_name:
                openActivity(ShopInfoActivity.class);
                break;
            case R.id.tv_shop_info_follow:
                showShortToast("关注");
                break;
            case R.id.img_shop_info_delete:
                finish();
                break;
            case R.id.ll_shop_peice_select:
                if (oldType == 2) {
                    setFilterTitleType(3);
                } else {
                    setFilterTitleType(2);
                }
                break;
            case R.id.tv_shop_buy_select:
                if (oldType != 0) {
                    setFilterTitleType(0);
                }
                break;
            case R.id.tv_shop_fiter_select:
                if (oldType != 1) {
                    setFilterTitleType(1);
                }
                break;
        }
    }

    private int oldType = 2;// 0默认 1销量 2价格

    private void setFilterTitleType(int type) {
        tv_shop_price_select.setSelected(type == 2 || type == 3);
        img_select_price_state.setSelected(type == 2 || type == 3);
        tv_shop_buy_select.setSelected(type == 0);
        tv_shop_fiter_select.setSelected(type == 1);
        if (type == 2 || type == 3) {
            if (type == 2) {
                // 高到底
                img_select_price_state.setImageResource(R.drawable.ic_pop_down);
            } else {
                // 低到高
                img_select_price_state.setImageResource(R.drawable.ic_pop_up);
            }
        } else {
//            setFilterStyle(type);
            img_select_price_state.setImageResource(R.drawable.ic_pop_down);
        }
        oldType = type; //type赋值给oldType
//        setFilterStyle(oldType);
    }

    @Override
    public void onShopInfo(ShopInfoBean xhxnInfoBean) {
        srf_rv_shop_info_detail.finishRefresh();
        if (xhxnInfoBean != null) {
            ltp_shop_info_detail.setLoadingTip(LoadingTip.LoadStatus.finish);
            this.mShopInfoBean = xhxnInfoBean;
            initWidget();
        } else {
            ltp_shop_info_detail.setLoadingTip(LoadingTip.LoadStatus.empty);
        }
    }

    /**
     * 控件数据
     */
    private void initWidget() {
//        ntb_shop_info.setTitleText(mShopInfoBean.getStore_name());
//        tv_store_open_time.setText("营业时间：" + mShopInfoBean.getBusiness_start_datetime() + " - " + mShopInfoBean.getBusiness_end_datetime());
    }

    @Override
    public void showMessage(String message, String method) {
        stopProgressDialog();
        showShortToast(message);
    }

    @Override
    public void showErrorView(String msg) {
        showErrorView(msg, ltp_shop_info_detail);
        ltp_shop_info_detail.setOnReloadListener(new LoadingTip.onReloadListener() {
            @Override
            public void reload(View view) {
                ltp_shop_info_detail.setLoadingTip(LoadingTip.LoadStatus.loading);
                fetchShopInfoDetails();
            }
        });
    }
}